package extract.knowledge.test;

import org.apache.tinkerpop.gremlin.structure.Graph;
import org.apache.tinkerpop.gremlin.structure.T;
import org.apache.tinkerpop.gremlin.structure.Vertex;
import org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerGraph;
import org.apache.tinkerpop.gremlin.util.iterator.IteratorUtils;

/**
 * Created by Common on 7/26/16.
 */
public class TinkerGraphExample {

    private Graph graph;

    public TinkerGraphExample(){
         this.graph = TinkerGraph.open();
    }



    public void createGraph(){
        Vertex marko = graph.addVertex(T.label, "person", T.id, 1, "name", "marko", "age", 29);
        Vertex vadas = graph.addVertex(T.label, "person", T.id, 2, "name", "vadas", "age", 27);
        Vertex lop = graph.addVertex(T.label, "software", T.id, 3, "name", "lop", "lang", "java");
        Vertex josh = graph.addVertex(T.label, "person", T.id, 4, "name", "josh", "age", 32);
        Vertex ripple = graph.addVertex(T.label, "software", T.id, 5, "name", "ripple", "lang", "java");
        Vertex peter = graph.addVertex(T.label, "person", T.id, 6, "name", "peter", "age", 35);
        marko.addEdge("knows", vadas, T.id, 7, "weight", 0.5f);
        marko.addEdge("knows", josh, T.id, 8, "weight", 1.0f);
        marko.addEdge("created", lop, T.id, 9, "weight", 0.4f);
        josh.addEdge("created", ripple, T.id, 10, "weight", 1.0f);
        josh.addEdge("created", lop, T.id, 11, "weight", 0.4f);
        peter.addEdge("created", lop, T.id, 12, "weight", 0.2f);
    }

    public void mutateGraph(){
        // add a software vertex with a name property
        Vertex gremlin = graph.addVertex(T.label, "software",
                "name", "gremlin");
        // only one vertex should exist
        assert(IteratorUtils.count(graph.vertices()) == 1);
        // no edges should exist as none have been created
        assert(IteratorUtils.count(graph.edges()) == 0);
        // add a new property
        gremlin.property("created",2009);
        // add a new software vertex to the graph
        Vertex blueprints = graph.addVertex(T.label, "software",
                "name", "blueprints");
        // connect gremlin to blueprints via a dependsOn-edge
        gremlin.addEdge("dependsOn",blueprints);
        // now there are two vertices and one edge
        assert(IteratorUtils.count(graph.vertices()) == 2);
        assert(IteratorUtils.count(graph.edges()) == 1);
        // add a property to blueprints
        blueprints.property("created",2010);
        // remove that property
        blueprints.property("created").remove();
        // connect gremlin to blueprints via encapsulates
        gremlin.addEdge("encapsulates",blueprints);
        assert(IteratorUtils.count(graph.vertices()) == 2);
        assert(IteratorUtils.count(graph.edges()) == 2);
        // removing a vertex removes all its incident edges as well
        //blueprints.remove();
        //gremlin.remove();
        // the graph is now empty
        System.out.println(IteratorUtils.count(graph.vertices()) );
        // tada!
    }

    /**********************
     *
     * @param args
     */
    public static void main(String[] args){
        TinkerGraphExample tg_eg = new TinkerGraphExample();
        //tg_eg.createGraph();
        tg_eg.mutateGraph();
        System.out.println("finished!");
    }
}
